import axios from "@/util/http";

export default {
  state: {
    is_predetermine: false,
    order_list: [],
    unpaid_order_list: [],
  },
  getters: {},
  mutations: {
    setIsPredetermine(state, flag) {
      state.is_predetermine = flag;
    },
    setOrderList(state, data) {
      state.order_list = data;
    },
    addOrderToUnpaidList(state, data) {
      state.unpaid_order_list.push(data);
    },
  },
  actions: {
    add_order_and_items(context, data) {
      const url = "/order/add/order/and/items/";
      const postData = data.confirmForm;
      const config = {
        headers: {
          Authorization: "Bearer " + postData.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          if (resp.data.code == 200) {
            data.success(resp.data);
          } else {
            data.error(resp.data);
          }
        })
        .catch((resp) => {
          console.log(resp.data);
        });
    },
    get_order_by_client_id(context, data) {
      const url = "/order/get/order/by/client/id/";
      const config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, data, config)
        .then((resp) => {
          context.state.order_list = resp.data.data;
        })
        .catch((resp) => {
          console.log(resp);
        });
    },
    get_all_unpaid_orders(context, data) {
      const url = "/order/get/all/unpaid/orders/";
      const config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios.post(url, data, config).then((resp) => {
        context.state.unpaid_order_list = resp.data.data;
      });
    },
    order_check_out(context, data) {
      const url = "/order/check/out/";
      const config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      data.token = null;
      axios.post(url, data, config).then((resp) => {
        data.success(resp.data);
      });
    },
    get_total_money(context, data) {
      const url = "/order/get/total/money/by/period/";
      const config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      data.token = null;
      axios.post(url, data, config).then((resp) => {
        data.success(resp.data);
      });
    },
  },
  modules: {},
};
